#include "Bank.h"
#include "quickSort.h"
#include <string>

CallerMachine& Bank::getCallerMachine() {
	return this->caller;
}
//bool Widgets::operator <=(const Widgets& other)const {
//	return this->num <= other.num;
//}
//bool Widgets::operator >=(const Widgets& other)const {
//	return this->num >= other.num;
//}

// 堆调整
void Bank::heapAdjust(WidgetType type) {
	int n = 10;
	//if (type == WidgetType::COMMON) {
	//	Widgets temp = widgets[1];
	//	widgets[1] = widgets[n];
	//	widgets[n] = temp;
	//}
	if (type == WidgetType::COMMON) {
		int k = 1;
		widgets[0] = widgets[k];
		for (int j = 2 * k; j <= n; j *= 2) {
			if (j<n && widgets[j].widget.FinTime()>widgets[j + 1].widget.FinTime())j++;
			if (widgets[0].widget.FinTime() <= widgets[j].widget.FinTime())break;
			else {
				widgets[k] = widgets[j];
				k = j;
			}
		}
		widgets[k] = widgets[0];
	}
	else {
		for (int i = n / 2; i > 0; i--) {
			int k = i;
			widgets[0] = widgets[k];
			for (int j = 2 * k; j <= n; j *= 2) {
				if (j<n && widgets[j].widget.FinTime()>widgets[j + 1].widget.FinTime())j++;
				if (widgets[0].widget.FinTime() <= widgets[j].widget.FinTime())break;
				else {
					widgets[k] = widgets[j];
					k = j;
				}
			}
			widgets[k] = widgets[0];
		}
	}
	//更新堆
	
}
//int Bank::getClients() {
//	Client client[11];
//	for (int i = 1; i <= 10; i++) {
//		Client temp(this->getCallerMachine());
//		cout << temp.getNo() << endl;
//		cout << temp.getReachTime() << endl;
//		client[i] = temp;
//	}
//	quickSort<Client> q(client, 10);
//	q.qsort(1, 10);
//	CallerMachine caller;
//	for (int i = 1; i <= q.getSize(); i++) {
//		client[i].setNo(caller.getCurrentNo());
//	}
//	for (int i = 1; i <= q.getSize(); i++) {
//		cout << client[i].getNo() << " " << client[i].getReachTime() << endl;
//	}
//	for (int i = 1; i <= q.getSize(); i++) {
//		
//		cout << "client type: "; client[i].getBussinessType() == BussinessType::COMMON ? cout << "COMMON" : cout << "SPECIAL";	cout << endl;
//		if (client[i].getBussinessType() == BussinessType::COMMON) {
//			this->widgets[1].widget.getQueue().push(client[i]);
//			int subtract = widgets[1].widget.FinTime() - client[i].getReachTime() > 0 ? widgets[1].widget.FinTime() - client[i].getReachTime() : 0;
//			client[i].setWaitingTime(subtract);
//			this->widgets[1].widget.FinTime() = client[i].getOverTime();
//			this->widgets[1].widget.getCurNum() = client[i].getNo();
//			this->widgets[1].widget.setNumInQu() += 1;
//			heapAdjust(WidgetType::COMMON);
//		}
//		else {
//			int mini = 0x3f3f3f3f;
//			int mininum = -1;
//			for (int i = 1; i <= 10; i++) {
//				if (this->widgets[i].type == WidgetType::SPECIAL && mini > this->widgets[i].widget.FinTime()) {
//					mininum = this->widgets[i].num;
//					mini = this->widgets[i].widget.FinTime();
//				}
//			}
//			this->widgets[mininum].widget.getQueue().push(client[i]);
//			int subtract = widgets[mininum].widget.FinTime() - client[i].getReachTime() > 0 ? widgets[mininum].widget.FinTime() - client[i].getReachTime() : 0;
//			client[i].setWaitingTime(subtract);
//			this->widgets[mininum].widget.FinTime() = client[i].getOverTime();
//			this->widgets[mininum].widget.getCurNum() = client[i].getNo();
//			this->widgets[mininum].widget.setNumInQu() += 1;
//			heapAdjust(WidgetType::SPECIAL);
//		}
//
//		cout << "Before adjust";
//		for (int j = 1; j <= 10; j++)cout << widgets[j].widget.FinTime() << " ";
//		cout << endl;
//
//		cout << "After adjust";
//		for (int j = 1; j <= 10; j++)cout << widgets[j].widget.FinTime() << " ";
//		cout << endl;
//	}
//
//	quickSort<Widgets> q2(this->widgets, 10);
//	q2.qsort(1, 10);
//	for (int j = 1; j <= 10; j++) {
//		cout << "no. " << this->widgets[j].num << "is " << this->widgets[j].widget.FinTime() << " "; cout << "Type: "; this->widgets[j].type == WidgetType::COMMON ? cout << "COMMON" : cout << "SPECIAL";cout << endl;
//	}
//
//	return 0;
//}

// 获取窗口（结构体数组）
Widgets& Bank::getWidgets() {
	return this->widgets[0];
}